package com.qf.lanzhou.blog.api.pojo.vo;

/**
 * 后端发给前端的标准
 */
public class BaseRespVO<T> {
    /**
     * 默认是成功状态
     */
    private int code = CodeType.SUCCESS.getValue();
    //错误信息
    private String msg;
    //业务信息
    private T data;

    public enum CodeType {
        //状态码 成功：100 失败：200 未登录：300
        SUCCESS(100),
        FAILED(200);
        private int value;

        CodeType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    //单例模式（饿汉，绝对单例）
    //    public static BaseRespVO baseRespVO = new BaseRespVO();
    //
    //    public static BaseRespVO getBaseRespVO() {
    //        return baseRespVO;
    //    }
    //现无法封装,与单例模式冲突,有能力封装的可以与石建朋沟通,探讨学习
    //成功信息返回
    public BaseRespVO success(T data) {
        BaseRespVO result = new BaseRespVO<>();
        result.setData(data);
        return result;
    }

    //失败信息返回
    public BaseRespVO failed(String msg) {
        BaseRespVO resulet = new BaseRespVO<>();
        resulet.setData(CodeType.FAILED.getValue());
        resulet.setMsg(msg);
        return resulet;
    }

    @Override
    public String toString() {
        return "BaseRespVO{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public BaseRespVO() {
    }

    public BaseRespVO(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
